package com.ferry.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class IOUtils{

    /**
     * 获取文件
     * @param path
     * @return
     */
    public static File getFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            return file;
        } else {
            return null;
        }
    }


    /**
     * 获取URL
     * @param url
     * @return
     * @throws MalformedURLException
     */
    public static URL getURL(String url) throws MalformedURLException {
        return new URL(url);
    }


    /**
     * 流拷贝
     * @param inputStream
     * @param outputStream
     * @param buffer
     * @throws IOException
     */
    public static void copy(InputStream inputStream, OutputStream outputStream, int buffer) throws IOException{
        int len = 0;
        byte[] bytes = new byte[buffer];
        while ( (len = inputStream.read(bytes)) != -1 ) {
            outputStream.write(bytes,0,len);
        }
    }

    public static void copy(InputStream inputStream,OutputStream outputStream) throws IOException{
        copy(inputStream,outputStream,1024);
    }

}